<!--
 * @Description: 原料产品
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-05-08 16:50:52
 * @FilePath: /yuxiuproweb/src/views/goods/rawMaterial/index.vue
-->

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="品名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:material:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:material:remove']"
          >删除</el-button
        >
      </el-col>
      
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="materialList"
      @selection-change="handleSelectionChange"
      :max-height="maxHeight"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="品名" align="center" prop="name" width="150" />
      <el-table-column label="分类" align="center" prop="type">
        <template slot-scope="{ row }">{{
          row.type === "0" ? "面料" : row.type === "1" ? "辅料" : "0"
        }}</template>
      </el-table-column>
      <el-table-column label="销售型号" align="center" prop="salesModel" />
      <el-table-column label="销售别名" align="center" prop="salesAlias" />
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template slot-scope="{ row }">{{
          row.enableFlag === "Y" ? "是" : "否"
        }}</template>
      </el-table-column>
      <el-table-column label="最小起算" align="center" prop="minimum" />
      <el-table-column label="销售信息" align="center" prop="marketingInfo" />
      <el-table-column label="出厂价" align="center" prop="factoryPrice" />
      <el-table-column
        label="零裁经销价"
        align="center"
        prop="wholesalePrice1"
        width="100"
      />
      <el-table-column
        label="零裁零售价"
        align="center"
        prop="retailPrice1"
        width="100"
      />
      <el-table-column
        label="整卷经销价"
        align="center"
        prop="wholesalePrice2"
        width="100"
      />
      <el-table-column
        label="整卷零售价"
        align="center"
        prop="retailPrice2"
        width="100"
      />
      <el-table-column
        label="APP展示内容维护"
        align="center"
        min-width="130"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <!-- v-hasPermi="['md:curtain:edit']" -->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleMaintenance(scope.row)"
            >维护</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="130"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:material:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:material:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品原料对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="1100px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-card class="box-card grid-box-card" shadow="hover">
          <div slot="header">
            <span class="title">商品原料</span>
          </div>
          <el-row>
            <el-col :span="8">
              <el-form-item label="材料" prop="fabricId">
                <el-select
                  v-model="form.fabricId"
                  filterable
                  placeholder="请选择材料"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in fabricList"
                    :key="item.mdFabricId"
                    :label="item.name"
                    :value="item.mdFabricId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="分类" prop="type">
                <el-select
                  v-model="form.type"
                  filterable
                  placeholder="请选择分类"
                  style="width: 100%"
                >
                  <el-option label="面料" value="0" />
                  <el-option label="辅料" value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="品名" prop="name">
                <el-input v-model="form.name" placeholder="请输入品名" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="销售型号" prop="salesModel">
                <el-input
                  v-model="form.salesModel"
                  placeholder="请输入销售型号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="销售别名" prop="salesAlias">
                <el-input
                  v-model="form.salesAlias"
                  placeholder="请输入销售别名"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="是否启用" prop="enableFlag">
                <el-switch
                  v-model="form.enableFlag"
                  active-value="Y"
                  inactive-value="N"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="最小起算" prop="minimum">
                <el-input v-model="form.minimum" placeholder="请输入最小起算" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="销售信息" prop="marketingInfo">
                <el-input
                  v-model="form.marketingInfo"
                  placeholder="请输入销售信息"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-card class="box-card grid-box-card mt20" shadow="hover">
          <div slot="header">
            <span class="title">价格相关数据</span>
          </div>
          <el-row>
            <el-col :span="8">
              <el-descriptions
                class="open-right"
                direction="vertical"
                :column="2"
                border
              >
                <el-descriptions-item label="价格">单价</el-descriptions-item>
                <el-descriptions-item label="出厂价">
                  <el-form-item label-width="0" prop="factoryPrice">
                    <el-input-number
                      v-model="form.factoryPrice"
                      placeholder="请输入"
                      clearable
                      :min="0"
                      :controls="false"
                      :precision="2"
                    ></el-input-number>
                  </el-form-item>
                </el-descriptions-item>
              </el-descriptions>
            </el-col>
            <el-col :span="16">
              <el-descriptions
                class="ml20 open-right"
                direction="vertical"
                :column="3"
                border
              >
                <el-descriptions-item label="价格"
                  >零裁<span style="color: red"
                    >(默认价格)</span
                  ></el-descriptions-item
                >
                <el-descriptions-item label="经销价">
                  <el-form-item label-width="0" prop="wholesalePrice1">
                    <el-input-number
                      v-model="form.wholesalePrice1"
                      placeholder="请输入"
                      clearable
                      :min="0"
                      :controls="false"
                      :precision="2"
                    ></el-input-number>
                  </el-form-item>
                </el-descriptions-item>
                <el-descriptions-item label="零售价">
                  <el-form-item label-width="0" prop="retailPrice1">
                    <el-input-number
                      v-model="form.retailPrice1"
                      placeholder="请输入"
                      clearable
                      :min="0"
                      :controls="false"
                      :precision="2"
                    ></el-input-number>
                  </el-form-item>
                </el-descriptions-item>
                <el-descriptions-item label="价格">整卷</el-descriptions-item>
                <el-descriptions-item label="经销价">
                  <el-form-item label-width="0" prop="wholesalePrice2">
                    <el-input-number
                      v-model="form.wholesalePrice2"
                      placeholder="请输入"
                      clearable
                      :min="0"
                      :controls="false"
                      :precision="2"
                    ></el-input-number>
                  </el-form-item>
                </el-descriptions-item>
                <el-descriptions-item label="零售价">
                  <el-form-item label-width="0" prop="retailPrice2">
                    <el-input-number
                      v-model="form.retailPrice2"
                      placeholder="请输入"
                      clearable
                      :min="0"
                      :controls="false"
                      :precision="2"
                    ></el-input-number>
                  </el-form-item>
                </el-descriptions-item>
              </el-descriptions>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- app图片维护 -->
    <image-view
      v-if="image.show"
      :show.sync="image.show"
      v-bind="image"
      @update="getList"
    ></image-view>
  </div>
</template>

<script>
import {
  listRawMaterial,
  getRawMaterial,
  delRawMaterial,
  addRawMaterial,
  updateRawMaterial,
} from "@/api/goods/material";
import { listFabric } from "@/api/material/fabric";
import ImageView from "@/views/app/image/mods/add.vue";
import commonMix from "@/utils/mixins/common";

export default {
  name: "Material",
  mixins: [commonMix],
  components: {
    ImageView,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品原料表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        name: null,
        salesModel: null,
        salesAlias: null,
        enableFlag: null,
        minimum: null,
        marketingInfo: null,
        skuPicture: null,
        factoryPrice: null,
        wholesalePrice1: null,
        retailPrice1: null,
        wholesalePrice2: null,
        retailPrice2: null,
        fabricId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      fabricList: [],
      // app相关
      image: {
        show: false,
        type: "2",
        id: null,
      },
    };
  },
  created() {
    this.getList();
    this.getFabricList();
  },
  methods: {
    /** 查询面料列表 */
    getFabricList() {
      listFabric({
        pageNum: 1,
        pageSize: 500,
      }).then(({ rows }) => {
        this.fabricList = rows;
      });
    },
    /** 查询产品原料列表 */
    getList() {
      this.loading = true;
      listRawMaterial(this.queryParams).then((response) => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rawMaterialId: null,
        name: null,
        salesModel: null,
        salesAlias: null,
        enableFlag: "N",
        minimum: null,
        marketingInfo: null,
        skuPicture: null,
        factoryPrice: null,
        wholesalePrice1: null,
        retailPrice1: null,
        wholesalePrice2: null,
        retailPrice2: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        fabricId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.rawMaterialId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品原料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rawMaterialId = row.rawMaterialId || this.ids;
      getRawMaterial(rawMaterialId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品原料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.rawMaterialId != null) {
            updateRawMaterial(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRawMaterial(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rawMaterialIds = row.rawMaterialId || this.ids;
      this.$modal
        .confirm('是否确认删除产品原料编号为"' + rawMaterialIds + '"的数据项？')
        .then(function () {
          return delRawMaterial(rawMaterialIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 维护app数据 */
    handleMaintenance({ rawMaterialId }) {
      this.image = {
        show: true,
        type: "2",
        id: rawMaterialId,
      };
    },
   
    
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .el-form-item {
    input {
      text-align: left;
    }
  }
}
.open-right {
  ::v-deep {
    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
